package com.ybxx.system.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.util.CharSetUtil;
import com.ybxx.core.util.StringUtil;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.action.BaseAction;
import com.ybxx.system.SystemConstants;
import com.ybxx.system.model.DataRights;
import com.ybxx.system.model.Permission;
import com.ybxx.system.model.User;
import com.ybxx.system.service.UserService;

import flexjson.JSONSerializer;

public class UserAction extends BaseAction{
	@Resource(name="userService")
	private UserService userService;
	private User user;
	private List<User> users;
	private String msg="succ";
	private JSONObject resultObj;
	private String loginid;
	private String menuData;
	private String dataRights;
	/*删除用户*/
	public String remove(){
		try{
			userService.remove(loginid);
		}catch(Exception ex){
			msg="该用户删除失败,只能禁用，不能删除";
		}finally{
			return "succ";
		}
	}
	
	/*设置权限*/
	public String savePermission(){
		try{
			User tmp=userService.get(loginid);
			if(menuData!=null && !menuData.equals("")){
				String[] menus=menuData.split("!");
				Set<Permission> permissions=new HashSet<Permission>();
				for(String menu:menus){
					Permission permission=new Permission();
					permission.setPid(menu);
					permissions.add(permission);
				}
				tmp.setPermissions(permissions);
			}
			if(dataRights!=null && !dataRights.equals("")){
				String[] datas=dataRights.split("!");
				Set<DataRights> dataRightsex=new HashSet<DataRights>();
				for(String data:datas){
					DataRights rights=new DataRights();
					rights.setDcode(UUIDGenerator.getUUID());
					rights.setUser(tmp);
					rights.setDvalue(data.substring(1));
					rights.setDtype(data.substring(0,1));
					dataRightsex.add(rights);
				}
				tmp.setDataRightsex(dataRightsex);
			}
			userService.saveUserPermission(tmp);
			
		}catch(Exception ex){
			ex.printStackTrace();
			msg="用户权限设置失败";
		}finally{
			return "succ";
		}
	}
	/*设置密码*/
	public String savePwd(){
		msg="succ";
		try{
			User tmp=userService.get(user.getLoginid());
			tmp.setPwd(user.getPwd());
			userService.save(tmp);
		}catch(Exception ex){
			msg="用户密码设置失败";
		}finally{
			return "succ";
		}
	} 
	/*用于保存*/
	public String save(){
		msg="succ";
		try{
			User tmp=userService.get(user.getLoginid());
			if(tmp==null){
				userService.save(user);
			}else{
				tmp.setName(user.getName());
				tmp.setTel(user.getTel());
				userService.save(tmp);
			}
		}catch(Exception ex){
			msg="用户保存失败";
		}finally{
			return "succ";
		}
	}
	/*用于新增、浏览、修改加载数据*/
	public String load(){
		user=userService.get(user.getLoginid());
		return "loadUser";
	}
	/**用户管理，分页查询*/
	public String query(){
		QueryFilter filter = new QueryFilter(getRequest()); 
		this.users=this.userService.queryPage(filter, user==null?null:user.getName());
		super.setRownum(filter.getPagingBean().getTotalItems());
		super.setCurrentpage(Integer.parseInt(filter.getPagingBean().getStart()+"")/Integer.parseInt(filter.getPagingBean().getPageSize()+"")+1);
		super.setPageSize(Integer.parseInt(filter.getPagingBean().getPageSize()+""));
		return "query";
	}
	/**
	 * 系统登陆
	 * @return
	 */
	public String login(){
		
		try{
			msg = "succ";
			User currentUser = this.userService.login(user.getLoginid());
			if(currentUser==null){
				msg = "用户名不存在！";
			}else if(!user.getPwd().equals(currentUser.getPwd())){
				msg = "密码错误！";
			}
			if(msg.equals("succ")){
				super.getSession().setAttribute(SystemConstants.SESSION_CURRENT_USER, currentUser);
			}
		}catch(Exception e){
			e.printStackTrace();
			msg="登录异常，请稍后重试！";
		}finally{
			return "succ";
		}
		
	}
	
	
	
	/**
	 * 注销登陆
	 * @return
	 */
	public String logout()
	{
		super.getSession().invalidate();
		return "logout";
	}



	public User getUser() {
		return user;
	}



	public void setUser(User user) {
		this.user = user;
	}


	public String getMsg() {
		return msg;
	}



	public void setMsg(String msg) {
		this.msg = msg;
	}
	public JSONObject getResultObj() {
		return resultObj;
	}
	public List<User> getUsers() {
		return users;
	}
	public String getLoginid() {
		return loginid;
	}
	public void setLoginid(String loginid) {
		this.loginid = loginid;
	}
	public String getMenuData() {
		return menuData;
	}
	public void setMenuData(String menuData) {
		this.menuData = menuData;
	}
	public String getDataRights() {
		return dataRights;
	}
	public void setDataRights(String dataRights) {
		this.dataRights = dataRights;
	}
	
}
